package com.tiger.communal.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.tiger.common.utils.DateUtils;
import com.tiger.communal.domain.HydropowerSubmeter;
import com.tiger.communal.mapper.HydropowerMapper;
import com.tiger.communal.mapper.HydropowerSubmeterMapper;
import com.tiger.communal.service.IHydropowerSubmeterService;
import com.tiger.project.domain.Asset;
import com.tiger.project.domain.BookValue;
import com.tiger.project.mapper.AssetMapper;
import com.tiger.project.mapper.BookValueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 水电分表Service业务层处理
 *
 * @author standard
 * @date 2022-10-13
 */
@Service
public class HydropowerSubmeterServiceImpl implements IHydropowerSubmeterService {
    @Autowired
    private HydropowerSubmeterMapper hydropowerSubmeterMapper;
    @Autowired
    private HydropowerMapper hydropowerMapper;
    @Autowired
    private BookValueMapper bookValueMapper;
    @Autowired
    private AssetMapper assetMapper;

    /**
     * 查询水电分表
     *
     * @param id 水电分表主键
     * @return 水电分表
     */
    @Override
    public HydropowerSubmeter selectHydropowerSubmeterById(Long id) {
        return hydropowerSubmeterMapper.selectHydropowerSubmeterById(id);
    }

    /**
     * 查询水电分表列表
     *
     * @param hydropowerSubmeter 水电分表
     * @return 水电分表
     */
    @Override
    public List<HydropowerSubmeter> selectHydropowerSubmeterList(HydropowerSubmeter hydropowerSubmeter) {
        return hydropowerSubmeterMapper.selectHydropowerSubmeterList(hydropowerSubmeter);
    }

    /**
     * 新增水电分表
     *
     * @param hydropowerSubmeter 水电分表
     * @return 结果
     */
    @Override
    public int insertHydropowerSubmeter(HydropowerSubmeter hydropowerSubmeter) {
        return hydropowerSubmeterMapper.insertHydropowerSubmeter(hydropowerSubmeter);
    }

    /**
     * 修改水电分表
     *
     * @param hydropowerSubmeter 水电分表
     * @return 结果
     */
    @Override
    public int updateHydropowerSubmeter(HydropowerSubmeter hydropowerSubmeter) {
        return hydropowerSubmeterMapper.updateHydropowerSubmeter(hydropowerSubmeter);
    }

    /**
     * 批量删除水电分表
     *
     * @param ids 需要删除的水电分表主键
     * @return 结果
     */
    @Override
    public int deleteHydropowerSubmeterByIds(Long[] ids) {
        return hydropowerSubmeterMapper.deleteHydropowerSubmeterByIds(ids);
    }

    /**
     * 删除水电分表信息
     *
     * @param id 水电分表主键
     * @return 结果
     */
    @Override
    public int deleteHydropowerSubmeterById(Long id) {
        return hydropowerSubmeterMapper.deleteHydropowerSubmeterById(id);
    }

    /**
     * 选择关联资产
     * @param id 总表id
     * @return
     */
    @Override
    public List<Asset> getAssetList(Long id) {
        ArrayList<Asset> list = new ArrayList<>();
        String assetId = bookValueMapper.getAssetId(id);
        if (assetId != null){
            List<String> ids = Arrays.asList(assetId.split(","));
            List<String> id1 = hydropowerSubmeterMapper.getAssetId(id);
            List<String> collect = ids.stream().filter(item -> !id1.contains(item)).collect(Collectors.toList());
            for (String s : collect){
                Asset asset = assetMapper.selectAssetById(Long.valueOf(s));
                list.add(asset);
            }
        }
        return list;
    }

}
